﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;

namespace UFSSServerClasses
{
    public static class FriendAccess
    {
        public static bool addFriend(string username, string friendname)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("acceptfriend", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", DBAccess.getUserID(username)));
                    c.Parameters.Add(new SqlParameter("@friendid", DBAccess.getUserID(friendname)));
                    c.ExecuteNonQuery();
                    connection.Close();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool rejectFriend(string username, string friendname)
        {
            int relationID = getRelationID(username, friendname);
            if (DBAccess.getUserID(username) == -1 || DBAccess.getUserID(friendname) == -1)
            {
                return false;
            }
            try
            {
                if (relationID != -1)
                {
                    using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                    {
                        connection.Open();
                        SqlCommand c = new SqlCommand("rejectfriend", connection);
                        c.CommandType = System.Data.CommandType.StoredProcedure;
                        c.Parameters.Add(new SqlParameter("@relationid", relationID));
                        c.ExecuteNonQuery();
                        connection.Close();
                        return true;
                    }
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        public static int getRelationID(string username, string friendname)
        {
            int result = -1;
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("getrelationid", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", DBAccess.getUserID(username)));
                    c.Parameters.Add(new SqlParameter("@friendid", DBAccess.getUserID(friendname)));
                    SqlDataReader r = c.ExecuteReader();
                    while (r.Read())
                    {
                        result = r.GetInt32(0);
                    }

                    return result;
                }
            }
            catch
            {
                return -1;
            }
        }

        public static bool requestFriend(string username, string friendname)
        {
            try
            {
                int userid = DBAccess.getUserID(username);
                int friendid = DBAccess.getUserID(friendname);
                if (userid == -1 || friendid == -1)
                {
                    return false;
                }
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("requestfriend", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", userid));
                    c.Parameters.Add(new SqlParameter("@friendid", DBAccess.getUserID(friendname)));
                    int temp = c.ExecuteNonQuery();
                    if (temp < 0)
                        return false;
                    connection.Close();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool delMessage(int messID)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("delmessage", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@messageid", messID));
                    c.ExecuteNonQuery();
                    connection.Close();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool sendMessage(string username, string friendname, string message, string subject)
        {
            int relationid = getRelationID(username, friendname);
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("sendmessage", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", DBAccess.getUserID(username)));
                    c.Parameters.Add(new SqlParameter("@recieverid", DBAccess.getUserID(friendname)));
                    c.Parameters.Add(new SqlParameter("@relationid", relationid));
                    c.Parameters.Add(new SqlParameter("@messagecontent", message));
                    c.Parameters.Add(new SqlParameter("@subject", subject));
                    c.ExecuteNonQuery();
                    connection.Close();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static ArrayList getMessage(int userID)
        {
            ArrayList mess = new ArrayList();
            UFSSMessage m = null;
            string name;
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("getMessageContent", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", userID));
                    SqlDataReader r = c.ExecuteReader();
                    while (r.Read())
                    {
                        name = DBAccess.getUsername(r.GetInt32(2));
                        m = new UFSSMessage(r.GetInt32(0), r.GetInt32(1), name, r.GetDateTime(3), r.GetString(4), r.GetString(5));
                        mess.Add(m);
                    }
                    connection.Close();
                    return mess;
                }
            }
            catch
            {
                return null;
            }
        }

        public static bool editMessage(int messID, string messCont)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("editmessage", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@messageid", messID));
                    c.Parameters.Add(new SqlParameter("@messagecontent", messCont));
                    c.ExecuteNonQuery();
                    connection.Close();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static List<string> getFriends(int id)
        {
            List<string> friends = new List<string>();
            int friendid;
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("getfriends", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", id));
                    SqlDataReader r = c.ExecuteReader();
                    
                    while (r.Read())
                    {
                        friendid = r.GetInt32(0);
                        friends.Add(DBAccess.getUsername(friendid));
                    }
                    connection.Close();
                    return friends;
                }
            }
            catch
            {
                return null;
            }
        }

        public static List<string> getRequestedFriends(int id)
        {
            List<string> friends = new List<string>();
            int friendid;
            try
            {
                using (SqlConnection connection = new SqlConnection(DBAccess.getConString()))
                {
                    connection.Open();
                    SqlCommand c = new SqlCommand("getrequestedfriends", connection);
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.Parameters.Add(new SqlParameter("@userid", id));
                    SqlDataReader r = c.ExecuteReader();

                    while (r.Read())
                    {
                        friendid = r.GetInt32(0);
                        friends.Add(DBAccess.getUsername(friendid));
                    }
                    connection.Close();
                    return friends;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
